package net.mygwt.ui.client.widget.table;

import java.util.ArrayList;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.event.BaseEvent;

public class EditRowSelectionModel extends RowSelectionModel {

	private int editRow= -1;
	protected EditTableItem editItem;
	private ArrayList editors= new ArrayList();
	protected EditTable tableref;
	
	public EditRowSelectionModel(int style) {
		super(style);
	}
	
	protected void init(EditTable table) {
		super.init(table);
		tableref= table;
		table.addListener(Events.RowClick, this);
		table.addListener(Events.RowDoubleClick, this);
//		table.addListener(Events.KeyDown, this);
	}
	
	public void handleEvent(BaseEvent be) {
//		System.err.println("HANDLE ROW : " + be.rowIndex + " EDIT ROW IS : " + editRow);
		if(editRow >= 0) {
			//editing
			if(be.rowIndex != editRow  
				&& (be.type == Events.RowDoubleClick || be.type == Events.RowClick)) {
				closeEdit();
			} else if(be.type == Events.KeyDown) {
				int code = DOM.eventGetKeyCode(be.event);
		        switch (code) {
		          case KeyboardListener.KEY_ESCAPE:
		            cancelEdit();
		            break;
		          case KeyboardListener.KEY_ENTER:
		        	closeEdit();
		        	be.type= -1; //stop any more processing
		        	break;
		        }
			}
		} else {
			super.handleEvent(be);
		}
	}
	
	protected void openEdit(int row, EditTableItem item) {
		editRow= row;
		if(item != null) {
			editItem= item;
		} else {
			editItem= (EditTableItem) tableref.getSelectedItem();
		}
		if(editItem != null) {
			editItem.onMouseOut(null);
			for(int i=0; i < editItem.getTable().getColumnCount(); i++) {
				Widget editor;
				try {
					editor= (Widget) editors.get(i);
				} catch(Exception e) {
					//set default:
					editor = new TextBox();
					setEditor(i, editor);
				}
				
				editItem.edit(i, editor);
			}
		} else {
			System.err.println("ITEM IS NULL TO openEdit!");
		}
        if(tableref != null) {
          tableref.setEditing(true);
        }
	}
	
	protected void closeEdit() {
		if(editRow >= 0 && editItem != null) {
			for(int i=0; i < editItem.getTable().getColumnCount(); i++) {
				Widget editor= (Widget) editors.get(i);
				editItem.commitEdit(i, editor);
			}
			editRow= -1;
            if(tableref != null) {
              tableref.setEditing(false);
            }
		} else {
			System.err.println("NULL CLOSE EDIT ITEM");
		}
	}
	
	protected void cancelEdit() {
		if(editRow >= 0 && editItem != null) {
			for(int i=0; i < editItem.getTable().getColumnCount(); i++) {
				Widget editor= (Widget) editors.get(i);
				editItem.cancelEdit(i, editor);
			}
			editRow= -1;
            if(tableref != null) {
              tableref.setEditing(false);
            }
		} else {
			System.err.println("NULL EDIT ITEM CANCEL!");
		}
	}
	
	public void setEditor(int column, Widget editor) {
      if(column < editors.size()) {
        //replace existing
          editors.set(column, editor);
        } else {
          editors.add(column, editor);
        }
	}
	
	public Widget getEditor(int column) {
		return (Widget) editors.get(column);
	}
}
